package cn.itsource.user.service.impl;

import cn.itsource.basic.constant.PetHomeConstant;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.domain.dto.UserDto;
import cn.itsource.user.mapper.LoginInfoMapper;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    /**
     *
     * @param userDto   注册用户信息
     */
    @Override
    public void register(UserDto userDto) {
        //校验前端传递的数据
        checkUserDto(userDto);
        //将登录用户信息传入到LoginInfo对象中
        LoginInfo loginInfo=userDto2LoginInfo(userDto);
        //判断t_loginInfo表中是否已经存在该用户
        //保存LoginInfo对象到数据库
        loginInfoMapper.save(loginInfo);
        //将LoginInfo对象信息传入User对象中
        User user=loginInfo2User(loginInfo);
        //将user对象保存到数据库汇总
        userMapper.save(user);

    }

    private User loginInfo2User(LoginInfo loginInfo) {
        User user = new User();
        BeanUtils.copyProperties(loginInfo, user);
        //设置状态 0待激活 1正常
        user.setState(PetHomeConstant.OK);
        //设置loginInfo对象
        user.setLoginInfo(loginInfo);
        return user;
    }

    private LoginInfo userDto2LoginInfo(UserDto userDto) {
        LoginInfo loginInfo = new LoginInfo();
        //设置用户名
        loginInfo.setUsername(userDto.getPhone());
        //设置电话
        loginInfo.setPhone(userDto.getPhone());
        //设置颜值
        loginInfo.setSalt(StrUtils.getRandomString(4));
        //设置密码加密
        loginInfo.setPassword(MD5Utils.encrypByMd5(userDto.getPassword()+loginInfo.getSalt()));
        //设置为前端用户
        loginInfo.setType(true);
        return loginInfo;
    }

    private void checkUserDto(UserDto userDto) {
        //校验前端传入的数据是否为空
        if(StringUtils.isEmpty(userDto.getPhone())||StringUtils.isEmpty(userDto.getCode())||
                StringUtils.isEmpty(userDto.getPassword())|| StringUtils.isEmpty(userDto.getPasswordRepeat())){
            throw new RuntimeException("请填写完整信息");
        }
        //校验手机号码是否已经注册
        User user=userMapper.findUserByPhone(userDto.getPhone());
        if(user!=null){
            throw new RuntimeException("该手机号已经注册");
        }
        //校验验证码是否过期
        String oldCode = (String)redisTemplate.opsForValue().get(PetHomeConstant.REGISTER_VERIFICATION_CODE + userDto.getPhone());
        if(oldCode==null){
            throw new RuntimeException("你的验证码已经过期");
        }
        //验证验证码是否正确
        String code = oldCode.split(":")[0];
        if(!code.toUpperCase().equals(userDto.getCode().toUpperCase())){
            throw new RuntimeException("验证码不正确");
        }
        //校验两次密码是否相同
        if(!userDto.getPassword().equals(userDto.getPasswordRepeat())){
            throw  new RuntimeException("两次输入密码不一致");
        }
    }
}
